package com.example.hrmsaas.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hrmsaas.company.entity.Company;
import com.example.hrmsaas.company.entity.Department;
import com.example.hrmsaas.company.entity.DeptListResult;
import com.example.hrmsaas.company.mapper.CompanyMapper;
import com.example.hrmsaas.company.mapper.DepartmentMapper;
import com.example.hrmsaas.company.service.IDepartmentService;
import com.example.hrmsaas.company.vo.DeptVo;
import com.example.hrmsaas.system.utils.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 雷哥
 * @since 2021-11-02
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    private Log log = (Log) LogFactory.getLog(DepartmentServiceImpl.class);
    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private CompanyMapper companyMapper;

    /**
     * 获取列表
     * @param deptVoQueryMapper
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public DataGridView getDeptAll(DeptVo deptVoQueryMapper) {
        QueryWrapper<Department> models = new QueryWrapper<>();
        if (null != models) {
            //组装查询条件
            models.eq("is_del",'0');
            if(deptVoQueryMapper.getCompanyId() != ""){
                models.eq(StringUtils.isNotBlank(deptVoQueryMapper.getCompanyId()),"company_id",deptVoQueryMapper.getCompanyId());
            }
        } else {
            log.info("DeptVo为空");
        }
        models.orderByDesc("create_time");
        List<Department> list= this.departmentMapper.selectList(models);
        System.out.println(list);
        Company company = companyMapper.selectById(deptVoQueryMapper.getCompanyId());
        DeptListResult deptListResult = new DeptListResult(company,list);
        return new DataGridView(deptListResult);
    }

    /**
     * 新增部门
     * @param department
     * @return
     */
    @Override
    public boolean save(Department department)
    {
        try{
            this.departmentMapper.insert(department);
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }

    /**
     * 更新部门
     * @param department
     * @return
     */
    @Override
    public boolean EditDept(Department department) {
        try{
            this.getBaseMapper().updateById(department);
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }

    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @Override
    public boolean DelDeptById(String id) {
        try{
            Department department = new Department();
            department.setIsDel(1);
            department.setId(id);
            this.getBaseMapper().updateById(department);
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }

    /**
     * 根据id获取部门详情
     * @param id
     * @return
     */
    @Override
    public Department getDeptById(String id) {
        QueryWrapper<Department> models = new QueryWrapper<>();
        DeptVo departmentVo = new DeptVo();
        departmentVo.setId(id);
        if (null != departmentVo) {
            models.eq(StringUtils.isNotBlank(departmentVo.getId()), "id",departmentVo.getId());
        } else {
            log.info("departmentVo为空");
        }
        Department data = departmentMapper.selectOne(models);
        return data;
    }



}
